package com.how2java;

import com.how2java.pojo.Product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMybatis {
    static String resource = "mybatis-config.xml";
    static InputStream inputStream = null;
    static SqlSessionFactory sqlSessionFactory = null;
    static SqlSession session = null;

    static {

        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void testIf() { // if元素测试

        System.out.println("查询所有的");
        List<Product> ps = session.selectList("listProductIf");
        for (Product p : ps) {
            System.out.println(p);
        }

        System.out.println("模糊查询");
        // Map<String,Object> params = new HashMap<>();
        // params.put("name","a");
        Product params = new Product();//注意对比

        List<Product> ps2 = session.selectList("listProductIf", params);
        for (Product p : ps2) {
            System.out.println(p);
        }
    }

    private static void testWhere() { // where元素测试 where 1=1现在属于不良代码
        System.out.println("多条件查询");
        Map<String, Object> params = new HashMap<>();
        params.put("name", "a");
        params.put("price", "10");
        List<Product> ps2 = session.selectList("listProductWhere", params);
        for (Product p : ps2) {
            System.out.println(p);
        }
    }

    public static void main(String[] args) throws IOException {
        //testIf();
        //testWhere();
        //testChoose();
        //testForEach();
        testBind();
        session.commit();
        session.close();

    }

    private static void testBind() {

        Map<String, String> params = new HashMap();
        params.put("name", "product");

        List<Product> ps = session.selectList("listProductBind", params);
        for (Product p : ps) {
            System.out.println(p);
        }

    }

    private static void testForEach() {
        List<Integer> ids = new ArrayList();
        ids.add(1);
        ids.add(3);
        ids.add(5);

        List<Product> ps = session.selectList("listProductForEach", ids);
        for (Product p : ps) {
            System.out.println(p);
        }
    }

    private static void testChoose() {// choose-when-otherwise 元素测试
        Map<String, Object> params = new HashMap<>();
        // params.put("name","a");
        params.put("price", "10");
        List<Product> ps = session.selectList("listProductChoose", params);
        for (Product p : ps) {
            System.out.println(p);
        }

    }
}